package com.manor.paradise.activity

import android.content.Intent
import android.view.View
import com.gyf.immersionbar.ktx.immersionBar
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.lyb_activity_my_farm.*
import kotlinx.android.synthetic.main.lyb_activity_tm_level.*
import kotlinx.android.synthetic.main.lyb_activity_tm_level.mTitleBarLayout
import java.math.BigDecimal

class LybMyFarmActivity : AbsActivity() {
    override fun getLayoutId() = R.layout.lyb_activity_my_farm
    override fun main() {
        super.main()
        setTopTitle("我的庄园", titleTvColor = R.color.white, leftIv = R.drawable.back_white, bgColor = R.color.transparent, lineShow = false)
        immersionBar {
            statusBarColor(R.color.transparent)
            titleBar(mTitleBarLayout)
            statusBarDarkFont(false)
        }
        getUserInfo()
        setClick(mMyActiveIndexIv, mMyActiveDetailsIv, mMlAndPowerIv, mGongxianTv, mGongxianzhiTv)
    }

   private fun getUserInfo(){
       ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
           override fun onSuccess(code: Int, msg: String, info: String) {
               if (code != 200) {
                   ToastUtil.show(msg)
                   return
               }
               var infoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)

               GlideUtil.loadImage(infoBean.avatar, mParadiseAvatarIv, 50, 50)
               mMyFarmUserLevelLayout.setOnClickListener {
                   LybTmLevelActivity.launch(this@LybMyFarmActivity, infoBean)
               }

               val eStar = when {
                   infoBean.npc.compareTo(BigDecimal("1")) == 0 -> "5"
                   infoBean.npc.compareTo(BigDecimal("2")) == 0 -> "6"
                   else -> infoBean.star.toString()
               }


               setLevel(eStar)

               mNickNameTv.text = infoBean.userNikeName
               mGongxianTv.addBottomLine().content(infoBean.contribute)
               mTmNumberTv.content("淘米号 " + infoBean.serialNo)
           }

       })
    }

    private fun setLevel(star: String) {
        //(-1无 0准星 1一星 2二星 3三星)
        when (star) {
            "-1" -> {
                mLevelTv.content("无等级")
                mLevelIv.setBackgroundResource(R.drawable.star_no)
            }
            "0" -> {
                mLevelTv.content("一星农场主")
                mLevelIv.setBackgroundResource(R.drawable.star1_lighten_b)
            }
            "1" -> {
                mLevelTv.content("二星农场主")
                mLevelIv.setBackgroundResource(R.drawable.star2_lighten_b)
            }
            "2" -> {
                mLevelTv.content("三星农场主")
                mLevelIv.setBackgroundResource(R.drawable.star3_lighten_b)
            }
            "3" -> {
                mLevelTv.content("四星农场主")
                mLevelIv.setBackgroundResource(R.drawable.star4_lighten_b)
            }
            "5" -> {
                mLevelTv.content("区域合伙人")
                mLevelIv.setBackgroundResource(R.drawable.star5_lighten_b)
            }
            "6" -> {
                mLevelTv.content("城市合伙人")
                mLevelIv.setBackgroundResource(R.drawable.star6_lighten_b)
            }

        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v?.id){
            R.id.mMyActiveIndexIv ->{
                startActivity(Intent(this, LybActiveIndexActivity::class.java))
            }
            R.id.mMyActiveDetailsIv ->{
                startActivity(Intent(this, LybMyActiveActivity::class.java))
            }
            R.id.mMlAndPowerIv ->{
                startActivity(Intent(this, LybMiAndPowerActivity::class.java))
            }
            R.id.mGongxianzhiTv, R.id.mGongxianTv ->{
                LybActiveExplainActivity.launch(2)
            }
        }
    }
}